package com.mj.haier.fin.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.fin.domain.BdGvsItemconfig;
import com.mj.haier.fin.repository.BdGvsItemconfigRepository;
import com.mj.haier.fin.repository.search.BdGvsItemconfigSearchRepository;
import com.mj.haier.fin.web.rest.errors.BadRequestAlertException;
import com.mj.haier.fin.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing BdGvsItemconfig.
 */
@RestController
@RequestMapping("/api")
public class BdGvsItemconfigResource {

    private final Logger log = LoggerFactory.getLogger(BdGvsItemconfigResource.class);

    private static final String ENTITY_NAME = "microFinBdGvsItemconfig";

    private final BdGvsItemconfigRepository bdGvsItemconfigRepository;

    private final BdGvsItemconfigSearchRepository bdGvsItemconfigSearchRepository;

    public BdGvsItemconfigResource(BdGvsItemconfigRepository bdGvsItemconfigRepository, BdGvsItemconfigSearchRepository bdGvsItemconfigSearchRepository) {
        this.bdGvsItemconfigRepository = bdGvsItemconfigRepository;
        this.bdGvsItemconfigSearchRepository = bdGvsItemconfigSearchRepository;
    }

    /**
     * POST  /bd-gvs-itemconfigs : Create a new bdGvsItemconfig.
     *
     * @param bdGvsItemconfig the bdGvsItemconfig to create
     * @return the ResponseEntity with status 201 (Created) and with body the new bdGvsItemconfig, or with status 400 (Bad Request) if the bdGvsItemconfig has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/bd-gvs-itemconfigs")
    @Timed
    public ResponseEntity<BdGvsItemconfig> createBdGvsItemconfig(@RequestBody BdGvsItemconfig bdGvsItemconfig) throws URISyntaxException {
        log.debug("REST request to save BdGvsItemconfig : {}", bdGvsItemconfig);
        if (bdGvsItemconfig.getSourceNameCode() != null) {
            throw new BadRequestAlertException("A new bdGvsItemconfig cannot already have an SourceNameCode", ENTITY_NAME, "sourceNameCodeexists");
        }
        BdGvsItemconfig result = bdGvsItemconfigRepository.save(bdGvsItemconfig);
        bdGvsItemconfigSearchRepository.save(result);
        return ResponseEntity.created(new URI("/api/bd-gvs-itemconfigs/" + result.getSourceNameCode()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getSourceNameCode().toString()))
            .body(result);
    }

    /**
     * PUT  /bd-gvs-itemconfigs : Updates an existing bdGvsItemconfig.
     *
     * @param bdGvsItemconfig the bdGvsItemconfig to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated bdGvsItemconfig,
     * or with status 400 (Bad Request) if the bdGvsItemconfig is not valid,
     * or with status 500 (Internal Server Error) if the bdGvsItemconfig couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/bd-gvs-itemconfigs")
    @Timed
    public ResponseEntity<BdGvsItemconfig> updateBdGvsItemconfig(@RequestBody BdGvsItemconfig bdGvsItemconfig) throws URISyntaxException {
        log.debug("REST request to update BdGvsItemconfig : {}", bdGvsItemconfig);
        if (bdGvsItemconfig.getSourceNameCode() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        BdGvsItemconfig result = bdGvsItemconfigRepository.save(bdGvsItemconfig);
        bdGvsItemconfigSearchRepository.save(result);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, bdGvsItemconfig.getSourceNameCode().toString()))
            .body(result);
    }

    /**
     * GET  /bd-gvs-itemconfigs : get all the bdGvsItemconfigs.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of bdGvsItemconfigs in body
     */
    @GetMapping("/bd-gvs-itemconfigs")
    @Timed
    public List<BdGvsItemconfig> getAllBdGvsItemconfigs() {
        log.debug("REST request to get all BdGvsItemconfigs");
        return bdGvsItemconfigRepository.findAll();
    }

    /**
     * GET  /bd-gvs-itemconfigs/:sourceNameCode : get the "sourceNameCode" bdGvsItemconfig.
     *
     * @param sourceNameCode the sourceNameCode of the bdGvsItemconfig to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the bdGvsItemconfig, or with status 404 (Not Found)
     */
    @GetMapping("/bd-gvs-itemconfigs/{sourceNameCode}")
    @Timed
    public ResponseEntity<BdGvsItemconfig> getBdGvsItemconfig(@PathVariable String sourceNameCode) {
        log.debug("REST request to get BdGvsItemconfig : {}", sourceNameCode);
        Optional<BdGvsItemconfig> bdGvsItemconfig = bdGvsItemconfigRepository.findBySourceNameCode(sourceNameCode);
        return ResponseUtil.wrapOrNotFound(bdGvsItemconfig);
    }

    /**
     * DELETE  /bd-gvs-itemconfigs/:sourceNameCode : delete the "sourceNameCode" bdGvsItemconfig.
     *
     * @param sourceNameCode the sourceNameCode of the bdGvsItemconfig to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/bd-gvs-itemconfigs/{sourceNameCode}")
    @Timed
    public ResponseEntity<Void> deleteBdGvsItemconfig(@PathVariable String sourceNameCode) {
        log.debug("REST request to delete BdGvsItemconfig : {}", sourceNameCode);

        bdGvsItemconfigRepository.deleteBySourceNameCode(sourceNameCode);
        bdGvsItemconfigSearchRepository.deleteBySourceNameCode(sourceNameCode);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, sourceNameCode.toString())).build();
    }

    /**
     * SEARCH  /_search/bd-gvs-itemconfigs?query=:query : search for the bdGvsItemconfig corresponding
     * to the query.
     *
     * @param query the query of the bdGvsItemconfig search
     * @return the result of the search
     */
    @GetMapping("/_search/bd-gvs-itemconfigs")
    @Timed
    public List<BdGvsItemconfig> searchBdGvsItemconfigs(@RequestParam String query) {
        log.debug("REST request to search BdGvsItemconfigs for query {}", query);
        return StreamSupport
            .stream(bdGvsItemconfigSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .collect(Collectors.toList());
    }

}
